package com.explore.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.explore.demo.common.ResultCode;
import com.explore.demo.common.ServerResponse;
import com.explore.demo.pojo.Category;
import com.explore.demo.mapper.CategoryMapper;
import com.explore.demo.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaolainan
 * @since 2021-04-14
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService
{

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public ServerResponse addCategory(Category category)
    {
        if (category == null){
            return ServerResponse.createBySuccessMessage(ResultCode.CATEGORY_IS_NULL.getDescription(),
                                                         ResultCode.CATEGORY_IS_NULL.getCode(),
                                                         null);
        }
        return categoryMapper.insert(category) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.CATEGORY_INSERT_FAIL.getDescription(),
                                                     ResultCode.CATEGORY_INSERT_FAIL.getCode(),
                                                     null);
    }

    @Override
    public ServerResponse deleteCategoryByCategoryName(String categoryName)
    {
        if (categoryName == null){
            return ServerResponse.createBySuccessMessage(ResultCode.CATEGORY_NAME_IS_NULL.getDescription(),
                                                         ResultCode.CATEGORY_NAME_IS_NULL.getCode(),
                                                         null);
        }
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper
                .eq("category_name",categoryName);
        Category category = categoryMapper.selectOne(wrapper);
        UpdateWrapper<Category> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("deleted",1);
        return categoryMapper.update(category,updateWrapper) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.CATEGORY_DELETE_FAIL.getDescription(),
                                                     ResultCode.CATEGORY_DELETE_FAIL.getCode(),
                                                     null);
    }

    @Override
    public ServerResponse queryAll()
    {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper
                .ge("id",1);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     categoryMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse queryCategoryByCategoryName(String categoryName)
    {
        if (categoryName == null){
            return ServerResponse.createBySuccessMessage(ResultCode.CATEGORY_NAME_IS_NULL.getDescription(),
                                                         ResultCode.CATEGORY_NAME_IS_NULL.getCode(),
                                                         null);
        }
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper
                .eq("category_name",categoryName);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     categoryMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse updateCategory(Category category)
    {
        if (category == null){
            return ServerResponse.createBySuccessMessage(ResultCode.CATEGORY_IS_NULL.getDescription(),
                                                         ResultCode.CATEGORY_IS_NULL.getCode(),
                                                         null);
        }
        return categoryMapper.updateById(category) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.UPDATE_FAIL.getDescription(),
                                                     ResultCode.UPDATE_FAIL.getCode(),
                                                     null);
    }
}
